<?php

namespace app\common\model\app;

use think\Model;


class Adput extends Model
{





    // 表名
    protected $name = 'app_adput';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'mode_text',
        'arealist_text',
        'start_time_text',
        'end_time_text',
        'status_text'
    ];


    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });

        self::afterDelete(function ($row) {
            // 删除图片
            // $arr = model("news.BuyhouselearnAttachment")->where('news_id', $row['id'])->select();
            // if ($arr) {
            //     foreach ($arr as $k => $v) {
            //         //同步删除oss
            //         \think\Hook::listen("upload_delete", $v);
            //         $v->delete();
            //     }
            // }

            $imagesArr = explode(",",$row['ad_file']);
            foreach ($imagesArr as $k => $v) {
                $attachment = new \stdClass();
                $attachment->url = $v;
                //同步删除oss 内置连带删除本地文件
                // \think\Hook::listen("upload_delete", $attachment);

                // 删除本地文件
                delFileAction($v);
                // 同步删除OSS文件
                // delOssFileAction($path);
            }
        });
    }

    public function getModeList()
    {
        return ['0' => __('Mode 0'), '1' => __('Mode 1')];
    }

    public function getArealistList()
    {
        return ['0' => __('Arealist 0'), '1' => __('Arealist 1'), '2' => __('Arealist 2'), '3' => __('Arealist 3'), '4' => __('Arealist 4'), '5' => __('Arealist 5'), '6' => __('Arealist 6'), '7' => __('Arealist 7'), '8' => __('Arealist 8'), '9' => __('Arealist 9')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }


    public function getModeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['mode']) ? $data['mode'] : '');
        $list = $this->getModeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getArealistTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['arealist']) ? $data['arealist'] : '');
        $valueArr = explode(',', $value);
        $list = $this->getArealistList();
        return implode(',', array_intersect_key($list, array_flip($valueArr)));
    }


    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setArealistAttr($value)
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    protected function setStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function admin()
    {
        return $this->belongsTo('app\admin\model\Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function appadposition()
    {
        return $this->belongsTo('app\common\model\AppAdPosition', 'position_id', 'position_id', [], 'LEFT')->setEagerlyType(0);
    }
}
